﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management.Automation.Host;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Sagacious.Automation.Controls;

namespace Sagacious.Automation.Host
{
    class PowerShellHost : PSHost
    {
        private IPowerShellControl hostControl;

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Host"></param>
        public PowerShellHost(IPowerShellControl hostControl)
        {
            this.hostControl = hostControl;
            this.userInterface = new PowerShellHostUserInterface(this.hostControl);
        }

        #endregion Constructors

        #region Public Properties

        /// <summary>
        /// Return the culture information to use. This implementation returns a 
        /// snapshot of the culture information of the thread that created this object.
        /// </summary>
        private CultureInfo originalCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
        public override System.Globalization.CultureInfo CurrentCulture
        {
            get { return this.originalCultureInfo; }
        }

        /// <summary>
        /// Return the UI culture information to use. This implementation returns a 
        /// snapshot of the UI culture information of the thread that created this object.
        /// </summary>
        private CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
        public override System.Globalization.CultureInfo CurrentUICulture
        {
            get { return this.originalUICultureInfo; }
        }

        /// <summary>
        /// This implementation always returns the GUID allocated at instantiation time.
        /// </summary>
        private Guid instanceId = Guid.NewGuid();
        public override Guid InstanceId
        {
            get { return this.instanceId; }
        }

        /// <summary>
        /// Return a string that contains the name of the host implementation. Keep in 
        /// mind that this string may be used by script writers to identify when your 
        /// host is being used.
        /// </summary>
        public override string Name
        {
            get { return "SagaciousPowerShellConsole"; }
        }

        /// <summary>
        /// This sample does not implement a PSHostUserInterface component so this property 
        /// simply returns null.
        /// </summary>
        private PowerShellHostUserInterface userInterface;
        public override PSHostUserInterface UI
        {
            get { return userInterface; }
        }

        /// <summary>
        /// Return the version object for this application. Typically this should match the 
        /// version resource in the application.
        /// </summary>
        public override Version Version
        {
            get { return new Version(1, 0, 0, 0); }
        }

        #endregion Public Properties

        #region Public Overrides

        /// <summary>
        /// Not implemented by this example class. The call fails with a NotImplementedException 
        /// exception.
        /// </summary>
        public override void EnterNestedPrompt()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Not implemented by this example class. The call fails with a NotImplementedException 
        /// exception.
        /// </summary>
        public override void ExitNestedPrompt()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// This API is called before an external application process is started. Typically it is 
        /// used to save state so the parent can restore state that has been modified by a child 
        /// process (after the child exits). In this example, this functionality is not  needed so 
        /// the method returns nothing.
        /// </summary>
        public override void NotifyBeginApplication()
        {
            return;
        }

        /// <summary>
        /// This API is called after an external application process finishes. Typically it is used 
        /// to restore state that a child process may have altered. In this example, this functionality 
        /// is not  needed so the method returns nothing.
        /// </summary>
        public override void NotifyEndApplication()
        {
            return;
        }

        /// <summary>
        /// Indicate to the host application that exit has been requested. Pass the exit code that the 
        /// host application should use when exiting the process.
        /// </summary>
        /// <param name="exitCode">The exit code to use.</param>
        public override void SetShouldExit(int exitCode)
        {
            this.hostControl.ExitCode = exitCode;
            this.hostControl.ShouldExit = true;
        }

        #endregion Public Overrides
    }
}
